<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="!filerLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a :class="{ active: item.id === filterData.brands.selectedBrand }" href="javascript:;" v-for="item in filterData.brands" :key="item.id" @click="changeBrand(item.id)">{{ item.name }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a :class="{ active: prop.id === item.selectedProp }" href="javascript:;" v-for="prop in item.properties" :key="prop.id" @click="changeProp(item, prop.id)">{{ prop.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="800px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
    <XtxSkeleton class="item" width="600px" height="40px"  />
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const filterData = ref({})
    const filerLoading = ref(false)
    const route = useRoute()
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        filerLoading.value = true
        findSubCategoryFilter(route.params.id).then(data => {
          data.result.brands.selectedBrand = null
          data.result.brands.unshift({
            id: null,
            name: '全部'
          })
          data.result.saleProperties.forEach(item => {
            item.selectedProp = null
            item.properties.unshift({
              id: null,
              name: '全部'
            })
          })
          filterData.value = data.result
          filerLoading.value = false
        })
      }
    }, {
      immediate: true
    })
    const getFilterParams = () => {
      const obj = {
        brandId: null,
        attrs: []
      }
      obj.brandId = filterData.value.brands.selectedBrand
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedProp) {
          const prop = item.properties.find(prop => prop.id === item.selectedProp)
          obj.attrs.push({ groupName: item.name, attrName: prop.name })
        }
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }
    const changeBrand = (id) => {
      if (id === filterData.value.brands.selectedBrand) return
      filterData.value.brands.selectedBrand = id
      emit('filter-change', getFilterParams())
    }
    const changeProp = (item, id) => {
      if (id === item.selectedProp) return
      item.selectedProp = id
      emit('filter-change', getFilterParams())
    }
    return {
      filterData,
      filerLoading,
      changeBrand,
      changeProp
    }
  }
}
</script>

<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
    .xtx-skeleton {
      padding: 10px 0;
    }
  }
</style>
